<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="roleId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      show-footer
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="partyGroupName" title="所属组织机构" min-width="200" sortable />
        <template v-slot:header>
          <partyGroupSelect ref="partyGroupSelect" @setPartyGroupCode="setPartyGroupCode" style="width: 100%"/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="roleName" title="角色名称" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.roleName" placeholder="请输入角色名称" clearable />
        </template>
      </vxe-table-column>
<!--      <vxe-table-column>-->
<!--        <vxe-table-column field="type" title="角色类型" min-width="100" :formatter="formatterType" sortable />-->
<!--        <template v-slot:header>-->
<!--          <el-select v-model="listQuery.type" clearable placeholder="请选择">-->
<!--            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />-->
<!--          </el-select>-->
<!--        </template>-->
<!--      </vxe-table-column>-->
      <vxe-table-column>
        <vxe-table-column field="description" title="备注" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.description" placeholder="请输入备注" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.type != 1" class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.type != 1" class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="授权" placement="top">
            <vxe-button type="text" @click="funcPermission(scope.row.code)">
              <i class="el-icon-setting" />
            </vxe-button>
          </el-tooltip>
<!--          <el-tooltip class="item" effect="dark" content="授权通知" placement="top">-->
<!--            <vxe-button type="text" @click="funcAuthorizeNotice(scope.row.code)">-->
<!--              <i class="el-icon-bell" />-->
<!--            </vxe-button>-->
<!--          </el-tooltip>-->
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="400" height="360" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="400" height="360" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelDetailFlag" title="详情" width="400" height="360" destroy-on-close resize>
      <compDetail ref="detailRef" :roleCode="roleCode" />
    </vxe-modal>
    <vxe-modal v-model="modelPermissionFlag" title="角色授权" width="450" height="750" resize destroy-on-close>
      <compPermission :code='code' @fatherCancelMethod="fatherCancel" />
    </vxe-modal>

  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/roleAdd.vue'
import compEdit from './components/roleEdit.vue'
import compDetail from './components/roleDetail.vue'
import compPermission from './components/rolePermission.vue'
import compNotice from './components/roleNotice.vue'
import request from '@/utils/request'
import partyGroupSelect from '@/views/components/partyGroupSelect.vue'
export default {
  name: '角色管理',
  components: {
    compAdd, compEdit, compDetail, compPermission, compNotice,partyGroupSelect
  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      partyGroup:[],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      modelNoticeFlag: false,
      modelPermissionFlag: false,
      copy: false,
      code:'',
      listQuery: {
        role: '',
        roleName: '',
        type: '',
        description: '',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      // 类型
      typeList: [
        { label: '管理员添加', value: 0 },
        { label: '内置角色', value: 1 }
      ],
      roleCode:''
    }
  },
  created() {
    // this.findList()
    this.getPartyGroup()
  },
  methods: {
    // 选择组织机构
    setPartyGroupCode(value) {
      this.listQuery.branchCode = value
    },
    //获取机构信息
    getPartyGroup() {
      request({
        url: '/arpa-basic-api/partyGroup/getPartyGroup',
        method: 'get'
      }).then(response => {
        this.partyGroup = response.data
        this.$refs.partyGroupSelect.initData(this.partyGroup, 'setPartyGroupCode')
      }).then(response => {
        this.findList()
      })
    },
    findList() {
      return new Promise(resolve => {
      this.loading = true
      request({
        url: '/arpa-basic-api/role',
        method: 'get',
        params: this.listQuery
      }).then(response => {
        this.tableData = response.data.records
        this.listQuery.total = response.data.total
        this.loading = false
        resolve()
      })
    })
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.reset();    //重置
      this.$refs.addRef.$refs.partyGroupSelect.initData(this.partyGroup, 'setPartyGroupCode')
      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.$refs.partyGroupSelect.initData(this.partyGroup, 'setPartyGroupCode')
      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(code) {
      this.roleCode = code
      this.modelDetailFlag = true
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm('确认删除吗？').then(_ => {
        request({ url: `/arpa-basic-api/role`, method: 'delete', params: { codes: row }}).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      }).catch(_ => {})
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
        case 'detail':
          this.modelDetailFlag = false
          break
        case 'permission':
          this.modelPermissionFlag = false
          break
        case 'notice':
          this.modelNoticeFlag = false
          break
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    // 翻译类型
    formatterType({ cellValue }) {
      const item = this.typeList.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    // 授权
    funcPermission(code) {
      this.code = code
      // this.$refs.permissionRef.getInfo(code)
      this.modelPermissionFlag = true
    },
    // 授权通知
    funcAuthorizeNotice(code) {
      this.$refs.noticeRef.getInfo(code)
      this.modelNoticeFlag = true
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
